<?php
/**
 * 1、采集铃声列表
 * 2、采集铃声内容 current
 */
include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Http.class.php');
include_once('../lib/function.php');

$redis = new Redis();  
$ret = $redis->connect("localhost","6379");  //php客户端设置的ip及端口
if(!$ret) die('redis connction faild');
$redis->auth(REDIS_PASS);
$redis->select(1);

$GLOBALS['config']['master']['db_database'] = 'mh_lnpan';
$GLOBALS['config']['slave']['db_database']  = 'mh_lnpan';

//$model = new Model('http://www.lnpan.com/index.php?m=api');
$model = new Db;
$Http  = new Http;

while (true) {
    $stop = $redis->get('LNPAN_QUEUE_STOP');
    if($stop) {
        sleep(3);
        die('stop');
    }

    $copy = $model->find("ring_news_copy", "state=0");
    if(empty($copy)) {
        echo2('ring_news_copy state=0 empty');
        sleep(3);
        continue;
    }

    $ringUrl = $copy['url'];
    $parse   = parse_url($ringUrl);
    $responses = $Http->curl_http( array($ringUrl) );
    if( empty($responses[$ringUrl]['error']) ) {
        $file = $responses[$ringUrl]['results'];
        if(empty($copy['type_name']) || 1) {
            preg_match('/<span><\/span>(.+?)<span><\/span>/i', $file, $match);
            $copy['type_name'] = strip_tags($match[1]);
        }
        //明星歌手
        preg_match('/<p>歌手：(.+?)<\/p>/i', $file, $match);
        preg_match_all('/<a[^>]*>(.+?)<\/a>/i', $match[1], $matches);
        $copy['singers'] = json_encode($matches[1]);

        //格式
        preg_match('/<div class="down">(.+?)<\/div>/is', $file, $match);
        $match[1] = str_replace('格式', '', $match[1]);
        preg_match_all('/<a[^>]*>(.+?)<\/a>/i', $match[1], $matches);
        $copy['format_name'] = json_encode($matches[1]);
        $format_name = $matches[1];

        //关键字
        preg_match('/<p>标签：(.+?)<\/p>/is', $file, $match);
        preg_match_all('/<a[^>]*>(.+?)<\/a>/i', $match[1], $matches);
        $tags = $matches[1];
        foreach ($tags as $k=>$tag) {
            if($tag == $copy['type_name'] || in_array($tag, $format_name)) {
                unset($tags[$k]);
            }
        }
        $tags = array_values($tags);
        $copy['tags'] = json_encode($tags);

        //下载js地址
        $down_data = array();
        preg_match('/<div class="down">(.+?)<\/div>/is', $file, $match);
        $match[1] = str_replace('格式', '', $match[1]);
        preg_match_all('/<a href="(.+?)"[^>]*>(.+?)<\/a>/i', $match[1], $matches);
        foreach($matches[1] as $k=>$item) {
            $tmp = array();
            $tmp['type'] = $matches[2][$k];
            $tmp['down_url'] = $item;
            $tmp['size'] = 0;
            $tmp['redirect_url'] = '';
            $down_data[] = $tmp;
        }
        $copy['down_info'] = json_encode($down_data);
        if(count($down_data) > 0) { //入临时库
            $copy['state'] = 2; //下载地址已有
            $model->save("ring_news_copy", "id='$copy[id]'", $copy);
        } else { //数据不全
            $copy['state'] = 12;
            $model->save("ring_news_copy", "id='$copy[id]'", $copy);
        }

        $info = array($copy['id'], $copy['url']);
    }
    else {
        $info = array($copy['id'], $copy['url'], $responses[$ringUrl]['error']);
    }

    $info['time'] = date('Y-m-d H:i:s');
    $redis->lPush('LNPAN_FETCH_RINGNEWS', serialize($info));
    
    print_r($copy);
    //sleep(10);
}
